﻿using msBsc.AppLogic.DataAlc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace msBsc.WebUi.wMall.Buyer
{
    public partial class wfTravelBags : msBsc.Cls.BPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //加载html
        protected void LoadHtml()
        {

            //判断是否登录
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.msg('请先登录后查看', { icon: 7 }, function () { location.href = '/wp_UserLogin.html';});");
                return;
            }

            string routeid = this.GetParamHtValue("routeid");
            string routetype = this.GetParamHtValue("routetype");
            if (string.IsNullOrEmpty(routeid) || string.IsNullOrEmpty(routetype))
            {
                this.ExecJScript("layer.alert('没有任何东西,请先选择路线', { icon: 7 });"); return;
            }
            string jsonTpl = "";
            //获取景点
            if (routetype == "2")
            {
                using (CBizDjkOrderRouteDetail routeDetail = new CBizDjkOrderRouteDetail())
                {
                    DataSet dsDetail = routeDetail.GetOrderRouteDetailList(routeid);
                    if (dsDetail == null || dsDetail.Tables[0].Rows.Count < 1)
                    {
                        return;
                    }

                    jsonTpl = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dsDetail.Tables[0], "", "", 1, 50, "");
                    this.ExecJScript(@"var _html = $('#jd_list_temp').html();
            laytpl(_html).render(" + jsonTpl + @", function (render) {
                $('#jd_list').html(render);$('#jd_list').prev().show();$('#jd_list_temp').remove();
            });");

                }
            }
            else if (routetype == "1")
            {
                //获取路线
                using (CBizDjkRouteBaseinfo route = new CBizDjkRouteBaseinfo())
                {
                    DataSet dsRoute = route.GetRouteInfoByID(routeid);
                    if (dsRoute == null || dsRoute.Tables[0].Rows.Count < 1)
                    {
                        return;
                    }

                    jsonTpl = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dsRoute.Tables[0], "", "", 1, 2, "");
                    this.ExecJScript(@"var _html = $('#route_list_temp').html();
            laytpl(_html).render(" + jsonTpl + @", function (render) {
                $('#route_list').html(render);$('#route_list').prev().show();$('#route_list_temp').remove();
            });");

                }

            }
            string carid = this.GetParamHtValue("carid");
            if (string.IsNullOrEmpty(carid))
            {
                this.ExecJScript("layer.alert('请先添加车辆', { icon: 7 });");
                return;
            }
            //获取车辆
            using (CBizVehicleInfo carinfo = new CBizVehicleInfo())
            {
                DataSet dsCar = carinfo.GetVehicleById(carid);
                if (dsCar == null || dsCar.Tables[0].Rows.Count < 1)
                {
                    return;
                }

                jsonTpl = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dsCar.Tables[0], "", "", 1, 2, "");

                this.ExecJScript(@"var _html = $('#car_list_temp').html();
                laytpl(_html).render(" + jsonTpl + @", function (render) {
                    $('#car_list').html(render);$('#car_list_temp').remove();
                });");
            }

        }

        //删除景点
        private void DeleteInfo()
        {
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript("layer.alert('登录失效，请重新登录', { icon: 7 });");
                return;
            }

            string detailId = this.GetParamHtValue("detailId");
            if (string.IsNullOrEmpty(detailId))
            {
                this.ExecJScript("layer.alert('该景点不存在', { icon: 7 });"); return;
            }

            using (CBizDjkOrderRouteDetail routeDetail = new CBizDjkOrderRouteDetail())
            {
                string message = routeDetail.UpdateStatus(detailId);
                if (message == "OK")
                {
                    this.ExecJScript("$('#r_" + detailId + "').remove();");
                }
                else
                {
                    this.ExecJScript("layer.alert('" + message + "', { icon: 7 });"); return;
                }
            }

        }

        //保存
        private void SaveBag()
        {

            //判断是否登录
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.msg('请先登录后才能提交', { icon: 7 }, function () { location.href = '/wp_UserLogin.html';});");
                return;
            }

            string routeid = this.GetParamHtValue("routeid");
            string routetype = this.GetParamHtValue("routetype");
            if (string.IsNullOrEmpty(routeid) || string.IsNullOrEmpty(routetype))
            {
                this.ExecJScript("layer.alert('没有任何东西,请先选择路线', { icon: 7 });"); return;
            }
            string carid = this.GetParamHtValue("carid");

            using (CBizDjkOrderService order = new CBizDjkOrderService())
            {
                string[] msgs = order.AddServiceInfo(routetype, routeid, carid, userid);
                if (Convert.ToBoolean(msgs[0]))
                {
                    this.ExecJScript("success('" + msgs[1] + "')");
                    return;
                }
                this.ExecJScript("layer.alert('" + msgs[1] + "', { icon: 7 });");
            }
        }
        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                LoadHtml();
            }
            else if (this.strAction == "delInfo")
            {
                DeleteInfo();
            }
            else if (this.strAction == "save")
            {
                SaveBag();
            }
        }
    }
}